package lcr;

import java.util.HashMap;

public class Test0010 {
}

class Solution0010_1 {
    public int subarraySum(int[] nums, int k) {
        int n = nums.length;
        int [] prefix = new int[n];
        prefix[0] = nums[0];
        for(int i=1;i<n;i++){
            prefix[i] = prefix[i-1] + nums[i];
        }
        HashMap<Integer,Integer> set = new HashMap<>();
        set.put(0,1);
        int ans =0;
        for(int i=0;i<n;i++){
            int target = prefix[i]-k;
            if(set.containsKey(target)){
                ans+= set.get(target);
            }
            set.merge(prefix[i],1,Integer::sum);
        }
        return ans;

    }
}